package pers.amos.dubbo.demo.export.server;

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.ServiceConfig;
import pers.amos.dubbo.demo.export.service.UserService;
import pers.amos.dubbo.demo.export.service.impl.UserServiceImpl;

import java.io.IOException;

/**
 * @author amos wong
 * @create 2020-10-24 7:49 上午
 *
 * 使用纯代码的方式启动Dubbo服务-生产者
 *
 * 1.配置：ApplicationConfig、ProtocolConfig、RegistryConfig、ServiceConfig
 * 2.暴露
 */

public class DubboServer {

    public static void main(String[] args) throws IOException {
        // application
        ApplicationConfig applicationConfig = new ApplicationConfig("dubbo-producer");

        // protocol 使用的协议，一般情况下都使用dubbo协议
        ProtocolConfig protocolConfig = new ProtocolConfig();
        protocolConfig.setName("dubbo");
        protocolConfig.setPort(-1); // 使用默认的端口20880

        // 设置注册中心，这里不使用注册中心，采用直连的方式
        RegistryConfig registryConfig = new RegistryConfig(RegistryConfig.NO_AVAILABLE);

        ServiceConfig serviceConfig = new ServiceConfig();
        serviceConfig.setInterface(UserService.class);
        serviceConfig.setRef(new UserServiceImpl());

        serviceConfig.setRegistry(registryConfig);
        serviceConfig.setApplication(applicationConfig);
        serviceConfig.setProtocol(protocolConfig);
        serviceConfig.export(); // 暴露服务

        System.out.println("服务已暴露");
        System.in.read();
    }


}
